package com.gzxxgl.user;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.gzxxgl.common.vo.Leavl;
import com.gzxxgl.enu.EnumMsg;
import com.gzxxgl.enu.TableSequence;
import com.gzxxgl.model.data.SysDict;
import com.gzxxgl.model.data.User;
import com.gzxxgl.model.data.UserLeave;
import com.gzxxgl.model.data.UserReward;
import com.gzxxgl.model.data.UserSalary;
import com.gzxxgl.util.Count;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

public class UserSalaryController extends Controller {

	public void find() {

		setAttr("dlsjjl", "查询工资信息");
		Map<String, Object> m = new HashMap<>();
		Map<String, String> con = new HashMap<>();
		m.put("code", 0);
		m.put("msg", EnumMsg.SUCCESS);

		Page<User> page = null;
		try {
			con.put("xm", getPara("xm"));
			page = User.dao.queryUsers(getParaToInt("pageIndex"), getParaToInt("pageSize"), con);

		} catch (Exception e) {
			e.printStackTrace();
			m.put("code", 1);
			m.put("msg", EnumMsg.ERR);
		}
		m.put("count", page.getTotalRow());
		m.put("list", page);

		renderJson(m);
	}

	@Before(Tx.class)
	public void update() {
		setAttr("dlsjjl", "更新工资信息");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", false);
		m.put("msg", EnumMsg.ERR);
		UserSalary us = UserSalary.dao.findById(getPara("id"));

		double gjgz = 0, zyzgbt = 0, txbt = 0, jtbt = 0, ccbt = 0, ybbt = 0, jbgz = 0, sgbt = 0, qtbt = 0, jxkhgz = 0,
				grkk = 0, ylbx = 0, dbbx = 0, ylbxx = 0, sybx = 0, gjj = 0, glgz = 0;
		if (StrKit.notBlank(getPara("gjgz"))) {
			us.set("gjgz", getPara("gjgz"));
		}
		if (StrKit.notBlank(getPara("zyzgbt"))) {
			us.set("zyzgbt", getPara("zyzgbt"));
			zyzgbt = Double.parseDouble(getPara("gjgz"));
		}
		if (StrKit.notBlank(getPara("txbt"))) {
			us.set("txbt", getPara("txbt"));
			txbt = Double.parseDouble(getPara("txbt"));
		}
		if (StrKit.notBlank(getPara("jtbt"))) {
			us.set("jtbt", getPara("jtbt"));
			jtbt = Double.parseDouble(getPara("jtbt"));
		}
		if (StrKit.notBlank(getPara("ccbt"))) {
			us.set("ccbt", getPara("ccbt"));
			ccbt = Double.parseDouble(getPara("ccbt"));
		}
		if (StrKit.notBlank(getPara("ybbt"))) {
			us.set("ybbt", getPara("ybbt"));
			ybbt = Double.parseDouble(getPara("ybbt"));
		}
		if (StrKit.notBlank(getPara("jbgz"))) {
			us.set("jbgz", getPara("jbgz"));
			jbgz = Double.parseDouble(getPara("jbgz"));
		}
		if (StrKit.notBlank(getPara("sgbt"))) {
			us.set("sgbt", getPara("sgbt"));
			sgbt = Double.parseDouble(getPara("sgbt"));
		}
		if (StrKit.notBlank(getPara("qtbt"))) {
			us.set("qtbt", getPara("qtbt"));
			qtbt = Double.parseDouble(getPara("qtbt"));
		}
		if (StrKit.notBlank(getPara("jxkhgz"))) {
			us.set("jxkhgz", getPara("jxkhgz"));
			jxkhgz = Double.parseDouble(getPara("jxkhgz"));
		}
		if (StrKit.notBlank(getPara("grkk"))) {
			us.set("grkk", getPara("grkk"));
			grkk = Double.parseDouble(getPara("grkk"));
		}
		if (StrKit.notBlank(getPara("gjj"))) {
			us.set("gjj", getPara("gjj"));
			gjj = Double.parseDouble(getPara("gjj"));
		}
		if (StrKit.notBlank(getPara("ylbx"))) {
			us.set("ylbx", getPara("ylbx"));
			ylbx = Double.parseDouble(getPara("ylbx"));
		}
		if (StrKit.notBlank(getPara("ylbxx"))) {
			us.set("ylbxx", getPara("ylbxx"));
			ylbxx = Double.parseDouble(getPara("ylbxx"));
		}
		if (StrKit.notBlank(getPara("dbbx"))) {
			us.set("dbbx", getPara("dbbx"));
			dbbx = Double.parseDouble(getPara("dbbx"));
		}
		if (StrKit.notBlank(getPara("sybx"))) {
			us.set("sybx", getPara("sybx"));
			sybx = Double.parseDouble(getPara("sybx"));
		}
		if (StrKit.notBlank(getPara("glgz"))) {
			us.set("glgz", getPara("glgz"));
			glgz = Double.parseDouble(getPara("glgz"));
		}

		boolean flag = us.update();

		double gxbxj = ylbx + ylbxx + dbbx + sybx + gjj,

				yfgz = gjgz + zyzgbt + txbt + jtbt + ccbt + ybbt + jbgz + sgbt + qtbt + jxkhgz + glgz + grkk;
		// getParaToInt("gjgz") +
		// getParaToInt("zyzgbt")+getParaToInt("txbt")+getParaToInt("jtbt")+getParaToInt("ccbt")+getParaToInt("ybbt")+getParaToInt("jbgz")+getParaToInt("sgbt")+getParaToInt("jxkhgz")+getParaToInt("grkk");

		double grsds = Count.tax(yfgz - gxbxj), grsdsj = 0;

		// 四舍五入
		grsds = ff(grsds);

		us.set("gxbxj", gxbxj).set("yfgz", yfgz).set("grsds", grsds).set("sfgz", ff(yfgz - gxbxj - grsds)).set("cz",
				ff(yfgz - gxbxj - grsds));

		us.update();

		UserReward ur = UserReward.dao.querybygid(getParaToInt("gid"));

		if (StrKit.notBlank(getPara("yfjj"))) {
			double yfjj = Double.parseDouble(getPara("yfjj"));
			ur.set("yfjj", getPara("yfjj"));
			if (yfjj > 0) {
				grsdsj = Count.tax((yfjj));
			}
			ur.set("grsds", ff(grsdsj));
			ur.set("sfjj", ff(yfjj - grsdsj));
		}
		boolean flagr = ur.update();

		if (flag && flagr) {

			m.put("flag", true);
			m.put("msg", EnumMsg.SUCCESS);
		}
		renderJson(m);
	}

	public void goSeniority() {

	}

	public void gonight() {
		setAttr("dlsjjl", "计算夜班补贴");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", true);
		m.put("msg", EnumMsg.SUCCESS);

		Integer num;
		try {
			num = getParaToInt("times") != null ? getParaToInt("times") : 0;
			m.put("count", night(num));
		} catch (Exception e) {
			e.printStackTrace();
			m.put("flag", false);
			m.put("msg", EnumMsg.ERR);
		}

		renderJson(m);
		;
	}

	public void overtime() {
		setAttr("dlsjjl", "计算加班补贴");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", true);
		m.put("msg", EnumMsg.SUCCESS);

		Integer num1, num2;
		try {
			num1 = getParaToInt("times1") == null ? 0 : getParaToInt("times1");
			num2 = getParaToInt("times2") == null ? 0 : getParaToInt("times2");
			m.put("count", overtime(num1, num2, getPara("gjgz")));
		} catch (Exception e) {
			e.printStackTrace();
			m.put("flag", false);
			m.put("msg", EnumMsg.ERR);
		}

		renderJson(m);
		;
	}

	public void leave() {

		setAttr("dlsjjl", "计算请假扣款");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", true);
		m.put("msg", EnumMsg.SUCCESS);

		Integer numsj, numbj, numhj, numcj;
		try {
			numsj = getParaToInt("sjtimes") == null ? 0 : getParaToInt("sjtimes");
			numbj = getParaToInt("bjtimes") == null ? 0 : getParaToInt("bjtimes");
			numhj = getParaToInt("hjtimes") == null ? 0 : getParaToInt("hjtimes");
			numcj = getParaToInt("cjtimes") == null ? 0 : getParaToInt("cjtimes");

			if (numsj > 0) {
				saveleave(22, getParaToInt("id"), numsj);
			}
			if (numbj > 0) {
				saveleave(23, getParaToInt("id"), numbj);
			}
			if (numhj > 0) {
				saveleave(25, getParaToInt("id"), numhj);
			}
			if (numcj > 0) {
				saveleave(26, getParaToInt("id"), numcj);
			}

			Leavl leavl = new Leavl(numbj, numhj, numsj, numcj);

			m.put("count", leave(leavl, getParaToInt("gjgz")));
		} catch (Exception e) {
			e.printStackTrace();
			m.put("flag", false);
			m.put("msg", EnumMsg.ERR);
		}

		renderJson(m);
		;

	}

	/**
	 * 生成本月工资条
	 */
	@Before(Tx.class)
	public void salarybill() {

		setAttr("dlsjjl", "生成本月工资条");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", false);
		m.put("msg", EnumMsg.ERR);

		String ids = getPara("ids");
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;

		String sql = " select *　from g_user_salary_data usd where usd.gid =? and usd.gyear=? and usd.gmonth=? and isdelete = 0 ";
		boolean flag = false;
		for (String s : ids.split(",")) {

			Record us = Db.findFirst("select * from g_user_salary where gid = ? ", s);

			Record ur = Db.findFirst("select * from g_user_reward where gid = ? ", s);

			if (us != null) {
				us.remove("salaryid");

				if (ur != null) {
					us.set("yfjj", ur.get("yfjj")).set("grsdss", ur.get("grsds")).set("sfjj", ur.get("sfjj"));
				}
				if(Double.parseDouble(us.get("sfgz")+"") < 0){
					us.set("sfgz", 0);
				}
				us.set("gyear", year).set("gmonth", month).set("gdate", new Timestamp(System.currentTimeMillis()));

				Record usa = Db.findFirst(sql, us.get("gid"), year, month);

				us.set("gyear", year).set("gmonth", month).set("gdate", new Timestamp(System.currentTimeMillis()));
				if (usa == null) {
					us.set("salarydataid", TableSequence.user_salary_data_id_sequence.name() + TableSequence.val);
					flag = Db.save("g_user_salary_data", "salarydataid", us);
				} else {
					us.set("salarydataid", usa.get("salarydataid"));
					flag = Db.update("g_user_salary_data", "salarydataid", us);
				}
			}
		}
		if (flag) {
			m.put("msg", EnumMsg.SUCCESS);
		}
		m.put("flag", flag);

		renderJson(m);

	}

	/**
	 * 工龄补贴
	 * 
	 * @throws ParseException
	 */
	@SuppressWarnings("unused")
	private static double Seniority(String date) throws ParseException {

		Integer yearTemp1 = 1, yearTemp2 = 4, yearTemp3 = 7, yearTemp4 = 10, yearTemp5 = 16, yearTemp6 = 20;

		double money1 = 20, money2 = 30, money3 = 50, money4 = 60, money5 = 80, money6 = 100;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date rzrq = sdf.parse(date);

		Calendar cal = Calendar.getInstance();

		int nyear = cal.get(Calendar.YEAR);

		cal.setTime(rzrq);

		int ryear = cal.get(Calendar.YEAR);
		int rmonth = cal.get(Calendar.MONTH) + 1;

		Integer year = null;

		year = nyear - ryear;
		if (rmonth >= 7) {
			if (year > 0) {
				year--;
			}
		}

		if (year >= yearTemp1 && year < yearTemp2) {
			return money1;
		} else if (year >= yearTemp2 && year < yearTemp3) {
			return money2;
		} else if (year >= yearTemp3 && year < yearTemp4) {
			return money3;
		} else if (year >= yearTemp4 && year < yearTemp5) {
			return money4;
		} else if (year >= yearTemp5 && year < yearTemp6) {
			return money5;
		} else if (year >= yearTemp6) {
			return money6;
		} else {
			return 0;
		}

	}

	/**
	 * 夜班补贴
	 * 
	 * @param s
	 * @return
	 */
	private static double night(Integer s) {
		double count = s * 20;
		return count;
	}

	/**
	 * 四舍五入
	 * 
	 * @param s
	 * @return
	 */
	public static double ff(double s) {
		BigDecimal b = new BigDecimal(s);
		double f = b.setScale(2, RoundingMode.HALF_UP).doubleValue();
		return f;
	}

	/**
	 * 加班补贴
	 * 
	 * @param weekend
	 *            周末加班
	 * @param fes
	 *            节假日加班
	 * @param gjgz
	 * @return
	 */
	public static double overtime(Integer weekend, Integer fes, Object gz) {
		double a, b, c, d, count, gjgz;
		gjgz = Double.parseDouble(gz + "");
		a = 21.75;
		b = ff(gjgz / a);
		c = 1;
		d = 2;
		count = weekend * b * c + fes * b * d;
		return ff(count);
	}

	/**
	 * 请假扣款
	 * 
	 * @param leavl
	 * @param gjgz
	 * @return
	 */
	public static double leave(Leavl leavl, double gjgz) {

		double bj = 1, sj = 0.3, hj = 1500, cj = 500, a = 21.75, count = 0;
		hj = hj / a;
		cj = cj / a;
		if (leavl.getSj() > 0) {

			count -= leavl.getSj() * bj * (gjgz / a);
		}
		if (leavl.getBj() > 0) {
			count -= leavl.getSj() * sj * (gjgz / a);
		}
		if (leavl.getHj() > 0) {
			count += (hj - gjgz / a) * leavl.getHj();
		}
		if (leavl.getCj() > 0) {
			count += leavl.getCj() * (cj - gjgz / a);
		}
		return ff(count);
	}

	public static boolean saveleave(Integer ltypeid, Integer gid, Integer num) {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		/**
		 * 存储请假数据
		 */
		boolean flag;
		UserLeave le = UserLeave.dao.querybyyearandmonth(year, month, gid);
		if (le == null) {
			Record r = new Record();

			r.set("leaveid", TableSequence.user_leave_id_sequence + TableSequence.val).set("gid", gid)
					.set("lyear", year).set("lmonth", month).set("ldate", new Timestamp(System.currentTimeMillis()))
					.set("ltype", SysDict.dao.querybyid(ltypeid).getDname()).set("ltypeid", ltypeid).set("ltimes", num);
			flag = Db.save("g_user_leave", "leaveid", r);
		} else {
			le.set("ldate", new Timestamp(System.currentTimeMillis()))
					.set("ltype", SysDict.dao.querybyid(ltypeid).getDname()).set("ltypeid", ltypeid).set("ltimes", num);
			flag = le.update();
		}
		return flag;
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(ff(24));
		System.out.println(ff(24.594));
		System.out.println(ff(24.596));
	}
}
